package controller;

import bean.dto.Dto;
import bean.pojo.GdAnswer;
import bean.pojo.GdFreequestion;
import bean.pojo.GdUser;
import bean.vo.DoctorVo;
import bean.vo.QuestionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import service.GdAnswerService;
import service.GdFreequestionService;
import service.TokenService;
import utils.DtoUtil;
import utils.RedisAPI;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 免费提问功能控制器
 */
@Controller
public class QuestionController {
    @Resource
    private RedisAPI redisAPI;
    @Resource
    private TokenService tokenService;
    @Resource
    private GdFreequestionService gdFreequestionService;
    @Resource
    private GdAnswerService gdAnswerService;

    /**
     * 提问
     * @param question
     * @param request
     * @return
     */
    @RequestMapping(value = "/freeAsk",method = RequestMethod.POST)
    public @ResponseBody
    Dto askQuestion(@RequestParam("question")String question,
                    @RequestParam("uploadFile") MultipartFile [] uploadFile,
                    HttpServletRequest request){
        String token = request.getHeader("token");
        GdUser gdUser = null;
        try {
            if (tokenService.validate(request.getHeader("user-agent"),token)){
                gdUser = tokenService.getUser(token);
                GdFreequestion gdFreequestion = new GdFreequestion();
                gdFreequestion.setQuestioninfo(question);
                gdFreequestion.setUserId(gdUser.getId());
                //判断file数组不能为空并且长度大于0   
                if(uploadFile!=null&&uploadFile.length>0){
                    //循环获取file数组中得文件     
                    for(int i = 0;i<uploadFile.length;i++){
                        MultipartFile file = uploadFile[i];
                        //保存文件       
                       saveFile(file,request);
                    //saveFile(uploadFile,request);
                   }
                }
            }
            return DtoUtil.returnFail("error","token失效");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","提问失败");
        }
    }
    /***
     * 保存文件
     * @param file
     * @return
     */
    private boolean saveFile(MultipartFile file,HttpServletRequest request) {
        // 判断文件是否为空
        if (!file.isEmpty()) {
            try {
                // 文件保存路径
                String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"
                        + file.getOriginalFilename();
                // 转存文件
                file.transferTo(new File(filePath));
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

}
